import React, { Component } from 'react'
import {ListBox} from '../../../components'
import UpdateRegionPage from './Update'
import { fetchData } from '../../../components/AjaxConsumer'
import { Modal } from 'antd';

export default class ListRegionPage extends Component {
    constructor(props) {
        super(props);
        this.state = {
            tableColumn: {},
            searchComponentData: []
        };
        //行操作列表
        this.rowOperation = [
            {
                handleName: 'edit',
                iconClass: 'cellEdit'
            }, {
                handleName: 'delete',
                iconClass: 'cellDele'
            }, {
                handleName: 'assignUser',
                iconClass: 'cellRole'
            }
        ];
        //header 操作列表
        this.headerOperation = [
            {
                handleName: 'addRegion',
                showName: '新增',
                iconClass: 'plus'
            },{
                handleName: 'edit',
                showName: '修改',
                iconClass: 'edit'
            }
            ,{
                handleName: 'delete',
                showName: '删除',
                iconClass: 'delete'
            }
        ];

        //table列表展示列配置
        //showName、columnName 必有属性， columnWidth 可选，默认100, columnHidden: true 列隐藏
        this.tableColumn = {
            storageKey: 'listRegionColumnConfig',
            data: [
                {
                    showName: 'regionId',
                    columnName: 'regionId',
                    columnHidden:true,
                    foreverHidden: true
                },
                {
                    showName: '区域编码',
                    columnName: 'regionCode',
                    columnWidth: 130
                }, {
                    showName: '区域描述',
                    columnName: 'regionDesc'
                },{
                    showName: '生产工厂编码',
                    columnName: 'productFactoryCode'
                },{
                    showName: '仓库编码',
                    columnName: 'whCode'
                }, {
                    showName: '激活标记',
                    columnName: 'activeFlag',
                    showOption:[
                        {
                            optionKey:'1',
                            optionShow:'启用'
                        },{
                            optionKey:'2',
                            optionShow:'禁用'
                        }
                    ]
                }, {
                    showName: '创建人',
                    columnName: 'createBy'
                }, {
                    showName: '创建时间',
                    columnName: 'gmtCreate',
                    columnWidth: 130
                }, {
                    showName: '最后修改人',
                    columnName: 'lastModifiedBy'
                }, {
                    showName: '最后修改时间',
                    columnName: 'gmtModified',
                    columnWidth: 130
                }
            ]
        };

        //搜索属性配置
        this.searchComponentData = [
            {
                name: 'activeFlag',
                type: 'select',
                optionList:[
                    {
                        optionKey:'',
                        optionShow:''
                    },
                    {
                        optionKey:'1',
                        optionShow:'启用'
                    },{
                        optionKey:'2',
                        optionShow:'禁用'
                    }
                ]
            },{
                name:'gmtCreate',
                type: 'date',
                maxName:'gmtCreateMax',
                minName:'gmtCreateMin',
                showTime: 1
            },{
                name:'gmtModified',
                type: 'date',
                maxName:'gmtModifiedMax',
                minName:'gmtModifiedMin',
                showTime: 1
            }
        ];

    }

    rowHandles = (strHandles, rowObj) => {
        console.log("strHandles===ListResource=======", strHandles);
        if (strHandles === 'edit') {
            this.editHandle(rowObj);
        } else if (strHandles === 'delete') {
            this.deleteHandle(rowObj);
        } else if (strHandles === 'addRegion') {
            this.addRegionHandle();
        } else if (strHandles == 'assignUser') {
            this.assignFactoryUserHandle(rowObj);
        }
    }
    // 增加区域
    addRegionHandle = () => {
        console.log("====执行了新增区域操作====");
        this.refs.updateRegion.getFormData[1].disable = false;
        this.refs.updateRegion.addRegionHandle();
    }
    editHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateRegion.getFormData[1].disable = true;
            this.refs.updateRegion.editHandle(rowObj[0].data);
        }
    }

    deleteHandle = (rowObj) => {
        if(rowObj.length == 0){
            Modal.error({
                title: '错误提示',
                content: '请选择一条数据',
            });
        }else if(rowObj.length > 1){
            Modal.error({
                title: '错误提示',
                content: '只能选择一条数据',
            });
        }else{
            this.refs.updateRegion.deleteHandle(rowObj[0].data)
        }
    }
    // 给区域分配角色
    assignFactoryUserHandle = (rowObj) => {
        let factoryId = rowObj.factoryId;
        this.refs.assignFactoryUser.initModal(factoryId);
    }


    //刷新列表
    refreshHandle = () => {
        this.refs.listRegion.refreshHandle();
    }

    componentDidMount() {
        const dicType = 'active_flag';
        const options= {
            url: '/base/dic/get/' + dicType,
            type: 'get',
            condition:{}
        };
        //先写死，后面再改
        //fetchData(this, options, this.dicInitCallBackHandle);
    }

    //下拉框 初始化
    dicInitCallBackHandle = (resData)=>{
        //alert('resData' + JSON.stringify(resData, null, 4));
        let optionList = [];
        let tmp = {};
        tmp.optionKey = '';
        tmp.optionShow = '请选择';
        optionList.push(tmp);
        resData.data.map( (e,index) => {
            let tmp = {};
            tmp.optionKey = e.dicCode;
            tmp.optionShow = e.dicName;
            optionList.push(tmp);
        });

        //alert('optionList' + JSON.stringify(optionList, null, 4));
        this.tableColumn.data[4].showOption = optionList;
        this.searchComponentData[0].optionList = optionList;
/*        this.setState({
            tableColumn: this.tableColumn,
            searchComponentData: this.searchComponentData
        })*/

    }

    render() {
        //当左侧隐藏时，列表宽度加上左侧的宽度，
        //1、当前this.props.isHideLeft 是从页面组件containers/Main/main.js传过来
        //2、this.props.isHideLeft 最顶级赋值是在页面 containers/Main/main.js
        let isHideLeft = this.props.isHideLeft;

        return (
            <div>
                <ListBox
                    ref='listRegion'
                    isHideLeft={isHideLeft}
                    title=" 区域管理"
                    tableColumnData={this.tableColumn}
                    searchComponentData={this.searchComponentData}
                    url='/base/region/list'
                    rowHandles={this.rowHandles}
                    rowOperation={this.rowOperation}
                    headerOperation={this.headerOperation}
                    isRowSingle = "true"
                />

                <UpdateRegionPage
                    ref="updateRegion"
                    refreshHandle={this.refreshHandle}
                />

            </div>
        )
    }
}
